/**
 * 控制器
 */

/**
 * 欢迎引导页面
 */
var WelcomeCtrl = function($scope, $rootScope) {
	var startApp = function() {
		/* 首次登陆 */
		ZGIT.setUserInit();

		$rootScope.gotoLogin();
	};

	if(ZGIT.UINIT) {
		startApp();
	} else {
		ZStore.getItem("UINIT", function(result) {
			if(result.success) {
				ZGIT.UINIT = (result.value.toUpperCase() === "TRUE");

				if(ZGIT.UINIT) {
					startApp();
				}
			}
		});
	}

	/* 立即体验 */
	$scope.gotoHome = function() {
		startApp();
	}

	$scope.slideHasChanged = function(index) {
		console.log("切换欢迎页面: " + index);
		ZStore.getItem("UINIT", function(result) {
			if(result.success) {
				ZGIT.UINIT = (result.value.toUpperCase() === "TRUE");
				if(ZGIT.UINIT) {
					startApp();
				}
			}
		});
	};
};

/**
 * 我的嗅微-首页
 */
var IndexHomeCtrl = function($rootScope, $scope) {
	/* 未登录 */
	if(!$rootScope.isLogined()) {
		$rootScope.gotoLogin();
		return;
	}
};

/**
 * 用户登录控制器
 */
var UserLoginCtrl = function($rootScope, $scope, $http, $timeout, $ionicLoading) {
	/* 已经登录 */
	if($rootScope.isLogined()) {
		$rootScope.gotoHome();
		return;
	}

	/* 登录表单 */
	$scope.uname = '15601656663';
	$scope.upwd = '123456';
	$scope.alogin = true;

	$scope.err = false;
	$scope.errMsg = '';

	/* 隐藏错误提示 */
	var waitHideErrMsg = function() {
		if($scope.err) {
			$timeout(function() {
				$scope.err = false;
			}, 2000);
		}
	};

	/* 用户输入 */
	$scope.onInput = function() {
		if($scope.err) {
			$scope.err = false;
		}
	};

	/* 表单提交 */
	$scope.doLogin = function() {
		if($scope.uname.length < 11) {
			$scope.err = true;
			$scope.errMsg = '手机号输入错误，请重新输入！';
		} else if($scope.upwd.length < 3) {
			$scope.err = true;
			$scope.errMsg = '登录密码输入错误，请重新输入！';
		}

		if($scope.err) {
			/* 隐藏错误提示 */
			waitHideErrMsg();
			return;
		}

		// Loading遮罩层
		$ionicLoading.show();

		/**/
		$http.jsonp(ZGIT.URL_BASE + "/jsonp/user-login.htm", {
			params: {
				callback: 'JSON_CALLBACK',
				uname: $scope.uname,
				upasswd: $scope.upwd
			}
		}).success(function(cdata) {
			$ionicLoading.hide();
			if(cdata.success) {
				ZGIT.setUserName($scope.uname);
				ZGIT.saveUserName($scope.alogin);
				$rootScope.gotoHome();
			} else {
				$scope.err = true;
				$scope.errMsg = cdata.errMsg;
				waitHideErrMsg();
			}
		});
		/**/
		/*
		 * $timeout(function() { ZGIT.setUserName($scope.uname); $rootScope.gotoHome(); }, 2000);
		 */
	};
};

/**
 * 我的社区-首页
 */
var ClubHomeCtrl = function($scope) {
};

/**
 * 我的社区-物业费缴纳
 */
var ClubPropFeeCtrl = function($scope) {
};

/**
 * 我的社区-车位费缴纳
 */
var ClubParkLotFeeCtrl = function($scope) {
};

/**
 * 我的社区-报修
 */
var ClubAskRepairCtrl = function($scope) {
	$scope.show = false;
	$scope.openCamera = function() {
		try {
			var cameraOptions = {
				quality: 50,
				destinationType: Camera.DestinationType.DATA_URL
			};

			var onSuccess = function(imageData) {
				var image = document.getElementById('myImage');
				image.src = "data:image/jpeg;base64," + imageData;
				$scope.show = true;
			};

			var onFailure = function(message) {
				ZGIT.log('拍照失败：' + message);
			};

			navigator.camera.getPicture(onSuccess, onFailure, cameraOptions);
		} catch(e) {
		}
	};
};

/**
 * 我的社区-房产管理
 */
var ClubPropMngtCtrl = function($scope) {
};

/**
 * 我的社区-车位管理
 */
var ClubParkLotMngtCtrl = function($scope) {
};

/**
 * 我的社区-我的工作流
 */
var ClubMyWorkFlowCtrl = function($scope) {
};

/**
 * 我的嗅微-首页
 */
var MineHomeCtrl = function($rootScope, $scope, $ionicPopup) {
	/* 安全退出 */
	$scope.doUserLogout = function() {
		var confirmPopup = $ionicPopup.confirm({
			title: '安全退出',
			subTitle: '安全退出后，原来登录数据将被清除！',
			template: '您确定安全退出登录系统吗？',
			okType: 'button-assertive',
			okText: '确定退出',
			cancelType: 'button-stable',
			cancelText: '取消'
		});

		confirmPopup.then(function(res) {
			if(res) {
				$rootScope.doLogout();
			} else {
				confirmPopup.close();
			}
		});
	};
};

/**
 * 我的嗅微-关于嗅微
 */
var AboutXWeiCtrl = function($scope) {
};

/**
 * 样例
 */
var ExampleCtrl = function($scope) {
	/* 通知 */
	$scope.notification = function() {
		console.log("LocalNotification start...");
		
		plugin.notification.local.setDefaults({
			autoCancel: true
		});

		plugin.notification.local.add({
			id: 1,
			message: 'I love BlackBerry!',
			json: JSON.stringify({
				test: 123
			})
		});

		plugin.notification.local.onclick = function(id, state, json) {
			console.log(id, JSON.parse(json).test);
		};
	};
};

/* */